// pages/writeArticle/writeArticle.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formats: {},
    readOnly: false,
    placeholder_title: '请输入标题',
    placeholder_idea:'请输入你的想法/问题',
    placeholder: '请输入正文',
    //edited页面内容
    dataArry: "",
    isFocus: false,
    isIdea:true
  },

  readOnlyChange() {
    this.setData({
      readOnly: !that.data.readOnly
    })
  },

  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },

  isFocus(e){
    let temp = true;
    if(this.data.isFocus){
      temp = false;
    }
    else temp = true;
    this.setData({
      isFocus: temp
    });
  },

  blur() {
    this.editorCtx.blur()
  },

  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)

  },

  insertImage() {
    const that = this
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.getpublish(res.tempFilePaths, 0)
      }
    })
  },

  // 递归循环下载图片
  getpublish(list, i) {
    wx.showLoading({
      title: '正在上传第' + (i + 1) + '张',
    })
    var that = this
    wx.uploadFile({
      url: "https://api110.herbplantist.com/sucai/public/index.php/post/post/uploadFile",
      filePath: list[i],
      name: 'file',
      formData: {
        key: 'Gn1xVdBiTClSSHKZifg0pTQSU5XGagWO',
        is_https: 1,
      },
      success(res) {
        var data = JSON.parse(res.data)
        console.log(data)
        if (data.status == 200) {
          that.editorCtx.insertImage({
            src: data.info.url,
            success: function () {}
          })
        }
        if (i + 1 == list.length) {
          wx.showToast({
            title: '上传成功',
          });
        }
        wx.hideLoading()
        if (++i < list.length) {
          that.getpublish(list, i);
        }
      },
    })
  },

  goplay(e) {
    this.setData({
      dataArry: e.detail.html
    })
  },

  sublimt() {
    let that = this
    wx.showModal({
      title: '温馨提示',
      content: '确定提交？',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '提交成功，打开调试看打印',
          })
          console.log(that.data.dataArry)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    let choice = options.choice;
    //如果选择了文章
    if(choice == "article"){
      that.setData({
        isIdea: false
      })
      wx.createSelectorQuery().select('#editor').context(function (res) {
            that.editorCtx = res.context;
            that.editorCtx.setContents({
              html: that.data.dataArry,
              success: function () {}
            })
          }).exec();
    }
    //如果选择了想法
    else if(choice == "idea"){
      that.setData({
        isIdea: true
      })
    }  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})